package com.anda.logistics.dao.impl;

import java.util.List;

import org.hibernate.transform.Transformers;
import org.hibernate.type.LongType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Component;

import com.anda.logistics.dao.SystemRoleDao;
import com.anda.logistics.model.RoleMenuRights;
import com.anda.logistics.model.SystemRole;
import com.anda.logistics.model.SystemUser;
import com.anda.logistics.model.SystemUserRole;
import com.anda.logistics.util.JQGridPager;
import com.anda.logistics.vo.UserRoleVo;
@Component
public class SystemRoleDaoImpl extends AndaBaseSuperDao implements SystemRoleDao {
	@Override
	public void save(SystemRole systemRole){
		sessionFactory.getCurrentSession().save(systemRole);
	}

	@Override
	public List<SystemRole> getPagerList(JQGridPager pager) {
		return pager.getPagerList(sessionFactory.getCurrentSession(), "SystemRole");
	}

	@Override
	public long getPagerRecords(JQGridPager pager) {
		return pager.getPagerRecords(sessionFactory.getCurrentSession(), "SystemRole");
	}

	@Override
	public void update(SystemRole sr) {
		sessionFactory.getCurrentSession().update(sr);		
	}

	@Override
	public SystemRole getOneById(int id) {
		return (SystemRole) sessionFactory.getCurrentSession().get(SystemRole.class, id);
	}

	@Override
	public void delete(SystemRole sr) {
		sessionFactory.getCurrentSession().delete(sr);
	}

	@Override
	public List<RoleMenuRights> getPagerList(JQGridPager pager, String tableName) {
		return pager.getPagerList(sessionFactory.getCurrentSession(), tableName);
	}

	@Override
	public long getPagerRecords(JQGridPager pager, String tableName) {
		return pager.getPagerRecords(sessionFactory.getCurrentSession(), tableName);
	}

	@Override
	public void save(RoleMenuRights rmr) {
		sessionFactory.getCurrentSession().save(rmr);		
	}

	@Override
	public void update(RoleMenuRights rmr) {
		sessionFactory.getCurrentSession().update(rmr);		
	}

	@Override
	public RoleMenuRights getOneRoleMenuById(int id) {
		return (RoleMenuRights) sessionFactory.getCurrentSession().get(RoleMenuRights.class, id);
	}

	@Override
	public void delete(RoleMenuRights rmr) {
		sessionFactory.getCurrentSession().delete(rmr);
	}

	@Override
	public int checkUser(SystemUser su) {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from SystemUser where userId = :userId or mobileNo = :mobileNo");
		long l =0;
		if (null!=su && su.getUserId() != null) {
			l= ((Number)sessionFactory.getCurrentSession()
				.createSQLQuery(sb.toString())
				.setString("userId", su.getUserId())
				.setString("mobileNo", su.getUserId())
				.uniqueResult()).longValue();
			if (l > 0) return 1;
		}
		
		if (null!=su && null!=su.getMobileNo()) {
			l = ((Number)sessionFactory.getCurrentSession()
					.createSQLQuery(sb.toString())
					.setString("userId", su.getMobileNo())
					.setString("mobileNo", su.getMobileNo())
					.uniqueResult()).longValue();
			if (l > 0) return 2;
		}
		
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserRoleVo> getUserRolePagerList(JQGridPager pager, SystemUser su) {
		StringBuffer sb = new StringBuffer();
		sb.append("select a.id,a.userId,a.roleId,b.roleName ")
		  .append("  from SystemUserRole a, SystemRole b ")
		  .append(" where a.roleId = b.id ")
		  .append("   and a.userId = :userId ")
		  .append(" order by a.id ");
		
		return sessionFactory.getCurrentSession()
				.createSQLQuery(sb.toString())
				.addScalar("id", LongType.INSTANCE)
				.addScalar("userId", LongType.INSTANCE)
				.addScalar("roleId", LongType.INSTANCE)
				.addScalar("roleName", StringType.INSTANCE)
				.setLong("userId", su.getId())
				.setResultTransformer(Transformers.aliasToBean(UserRoleVo.class))
				.setFirstResult((pager.getPage() -1) * pager.getRows())
				.setMaxResults(pager.getRows())
				.list();
	}

	@Override
	public long getUserRolePagerRecords(JQGridPager pager, SystemUser su) {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) ")
		  .append(" from SystemUserRole a, SystemRole b ")
		  .append(" where a.roleId = b.id ")
		  .append("   and a.userId = :userId ");
		return ((Number) sessionFactory.getCurrentSession()
				.createSQLQuery(sb.toString())
				.setLong("userId", su.getId())
				.uniqueResult())
				.longValue();
	}

	@Override
	public long getGrantedUserRoleCount(long userId, long roleId) {
		return ((Number)sessionFactory.getCurrentSession()
			.createSQLQuery("select count(*) from SystemUserRole where userId = :userId and roleId = :roleId")
			.setLong("userId", userId)
			.setLong("roleId", roleId)
			.uniqueResult())
			.longValue();
		
	}

	@Override
	public void grantRole(long userId, long roleId) {
		SystemUserRole sur = new SystemUserRole(userId, roleId);
		sessionFactory.getCurrentSession().save(sur);
	}

	@Override
	public SystemUser getSystemUser(String userId) {		
		return (SystemUser) sessionFactory.getCurrentSession()
				.createQuery("from SystemUser where userId = :userId or mobileNo = :mobileNo")
				.setString("userId", userId)
				.setString("mobileNo", userId)
				.setMaxResults(1)
				.uniqueResult()
				;
	}
}
